package fina2.dcs.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.log4j.Logger;

import fina2.dcs.common.xls.DCSException;
import fina2.dcs.common.xls.DCSExceptionConstants;

public class FileUtil {

	private static final int BUFFER_SIZE = 1024;
	private Logger log = Logger.getLogger(getClass());


	public synchronized int readFromZip(String zipName,Map<String, byte[]> filesToUpload, byte[] zipContent) throws IOException,DCSException {
		// File temp = new File("tmp");
		// DataOutputStream fosTemp = new DataOutputStream(new
		// ByteArrayOutputStream());
		// fosTemp.write(zipContent);
		// fosTemp.close();
		InputStream is = new DataInputStream(new ByteArrayInputStream(zipContent));
		ZipInputStream zis = new ZipInputStream(is);
		int entries = 0;
		// File f = null;
		// DataOutputStream fos = null;
		DataInputStream fis = null;
		ZipEntry ze = null;
		try {
			while ((ze = zis.getNextEntry()) != null) {
				entries++;
			}
			if (is != null)
				is.close();
			if (zis != null)
				zis.close();
			is = new DataInputStream(new ByteArrayInputStream(zipContent));
			zis = new ZipInputStream(is);

			if (entries == 1) {
				byte[] buffer = new byte[FileUtil.getBufferSize()];
				zis = new ZipInputStream(is);

				int read = 0;

				if ((ze = zis.getNextEntry()) != null) {
					String xlsName = ze.getName();
					
					
					// f = new File(name);
					ByteArrayOutputStream bos = new ByteArrayOutputStream();
					// fos = new DataOutputStream();
					while ((read = zis.read(buffer)) > 0) {
						bos.write(buffer, 0, read);
					}

					// fis = new DataInputStream(new ByteArrayInputStream(new
					// byte[fos.size()]));
					filesToUpload.put(xlsName, bos.toByteArray());
					
					if (!zipName.toLowerCase().trim().replace(".zip", ".xls").equals(xlsName.toLowerCase().trim())) {
						//throw new IllegalArgumentException(DCSExceptionConstants.DCS_ZIP_XLS_NAME_ERROR.getMessageCode());
						return DCSExceptionConstants.DCS_ZIP_XLS_NAME_ERROR.ordinal();
					}
				}

			} else {
				//throw new DCSException(DCSExceptionConstants.DCS_ZIP_NUMBER_FILES_ERROR.getMessageCode());
				return DCSExceptionConstants.DCS_ZIP_NUMBER_FILES_ERROR.ordinal();
			}
			
		}  finally {
			if (fis != null)
				fis.close();
			if (zis != null)
				zis.close();
			// if (f != null)
			// f.delete();
			// if (fos != null)
			// fos.close();
			// if (temp != null)
			// temp.delete();
			// if (fosTemp != null)
			// fosTemp.close();
			if (is != null)
				is.close();
			zis.close();
		}
		
		log.info("Files in zip " + entries);
		return -1;
	}

	public static byte[] toByteArray(InputStream input) throws IOException {

		ByteArrayOutputStream output = new ByteArrayOutputStream();
		copy(input, output);
		return output.toByteArray();
	}

	public static int copy(InputStream input, OutputStream output) throws IOException {

		byte[] buffer = new byte[BUFFER_SIZE];
		int count = 0;
		int n = 0;
		while (-1 != (n = input.read(buffer))) {
			output.write(buffer, 0, n);
			count += n;
		}
		return count;
	}

	public static int getBufferSize() {
		return BUFFER_SIZE;
	}

}
